<?php

namespace app\admin\controller;

use think\facade\Db;

class Meituan extends Backend
{

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIndexDataList()
    {
        $param = request()->param();
        $wheres = [];

        if (isset($param['add_time'])) {
            $add_time = date('Y-m-d', strtotime($param['add_time']));
            $end_time = date('Y-m-d', strtotime($param['end_time']));
            $wheres [] = ['pay_time', '>=', strtotime($add_time)];
            $wheres [] = ['pay_time', '<=', strtotime($end_time)];

        }
        if (isset($param['type'])) {
            $type = $param['type'];
        } else {
            $type = 1;
        }

        $where = [];
        if (isset($param['store_id'])) {
            $where[] = ['store_id', '=', $param['store_id']];
        }
        $list = Db::name('mini_room_list')
            ->alias('o')
            ->where($where)->field('o.id,o.type,o.room_name,o.price')->order('price desc')->page(PAGE, 10)
            ->select();

        $data = array();
        $bi = Db::name('mimi_fees')->where('id', 1)->find();

        foreach ($list as $i => $v) {
            $price = 0;
            switch ($type) {
                case 1:
                    $order = Db::name('mini_code_list')->where('admin_id', $v['id'])->select();
                    if (count($order)) {
                        foreach ($order as $m) {
                            $coupon = Db::name('coupon')->where('id', $m['crad_id'])->find();
                            if ($coupon) {
                                $price += $coupon['price'];
                            }
                        }
                    }

                    break;
                case 2:
                    $order = Db::name('mini_tilll_code_list')->where('admin_id', $v['id'])->select();
                    foreach ($order as $m) {
                        $coupon = Db::name('coupon')->where('id', $m['crad_id'])->find();
                        if ($coupon) {
                            $price += $coupon['price'];
                        }

                    }
                    break;
            }
            $v['price_count'] = 0;
            switch ($v['type']) {
                case 1:
                    $v['type_name'] = "自主加盟商户";
                    $v['price'] = $bi['corporation'];
                    $v['price_count'] = $price * $bi['corporation'];
                    break;
                case 2:
                    $v['type_name'] = "联营合作商户";
                    $v['price'] = $bi['li_corporation'];
                    $v['price_count'] = $price * $bi['li_corporation'];
                    break;
                case 3:
                    $v['type_name'] = "棋乐直营店";
                    $v['price'] = $bi['zi_corporation'];
                    $v['price_count'] = $price * $bi['zi_corporation'];
                    break;
                case 4:
                    $v['type_name'] = "外包店";
                    $v['price'] = $bi['w_corporation'];
                    $v['price_count'] = $price * $bi['w_corporation'];
                    break;
                case 0:
                    $v['type_name'] = " ";
                    $v['price'] = 0;

            }


            $data[] = array(
                'id' => $v['id'],
                'room_name' => $v['room_name'],
                'type_name' => $v['type_name'],
                'shop_price' => $price,
                'price' => $v['price'],
                'price_count' => $v['price_count'],

            );
        }
        $count = Db::name('mini_room_list')
            ->alias('o')
            ->where($where)->page(PAGE, 10)->count();

        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
            "count" => $count,
        );
        return $message;
    }


    public function getIndexData()
    {
        $param = request()->param();
        $wheres = [];

        if (isset($param['add_time'])) {
            $add_time = date('Y-m-d', strtotime($param['add_time']));
            $end_time = date('Y-m-d', strtotime($param['end_time']));
            $wheres [] = ['pay_time', '>=', strtotime($add_time)];
            $wheres [] = ['pay_time', '<=', strtotime($end_time)];

        }
        if (isset($param['type'])) {
            $type = $param['type'];
        } else {
            $type = 1;
        }

        $where = [];
        if (isset($param['store_id'])) {
            $where[] = ['store_id', '=', $param['store_id']];
        }
        $list = Db::name('mini_room_list')
            ->alias('o')
            ->where($where)->field('o.id,o.type,o.room_name,o.price')->order('price desc')->page(PAGE, 10)
            ->select();

        $data = array();
        $bi = Db::name('mimi_fees')->where('id', 1)->find();

        $price_count = 0;
        foreach ($list as $i => $v) {
            $price = 0;
            switch ($type) {
                case 1:
                    $order = Db::name('mini_code_list')->where('admin_id', $v['id'])->select();
                    if (count($order)) {
                        foreach ($order as $m) {
                            $coupon = Db::name('coupon')->where('id', $m['crad_id'])->find();
                            if ($coupon) {
                                $price += $coupon['price'];
                            }
                        }
                    }

                    break;
                case 2:
                    $order = Db::name('mini_tilll_code_list')->where('admin_id', $v['id'])->select();
                    foreach ($order as $m) {
                        $coupon = Db::name('coupon')->where('id', $m['crad_id'])->find();
                        if ($coupon) {
                            $price += $coupon['price'];
                        }

                    }
                    break;
            }
            $v['price_count'] = 0;
            switch ($v['type']) {
                case 1:
                    $v['type_name'] = "自主加盟商户";
                    $v['price'] = $bi['corporation'];
                    $v['price_count'] = $price * $bi['corporation'];
                    break;
                case 2:
                    $v['type_name'] = "联营合作商户";
                    $v['price'] = $bi['li_corporation'];
                    $v['price_count'] = $price * $bi['li_corporation'];
                    break;
                case 3:
                    $v['type_name'] = "棋乐直营店";
                    $v['price'] = $bi['zi_corporation'];
                    $v['price_count'] = $price * $bi['zi_corporation'];
                    break;
                case 4:
                    $v['type_name'] = "外包店";
                    $v['price'] = $bi['w_corporation'];
                    $v['price_count'] = $price * $bi['w_corporation'];
                    break;
                case 0:
                    $v['type_name'] = " ";
                    $v['price'] = 0;

            }

            $price_count += $v['price_count'];

        }

        $data['shopprice'] = round($price_count, 2);

        return message('成功', true, $data);
    }
}